<template>
  <div class="app-container">
    <!--    <div><h1 style="text-align: center">员工排班表</h1></div>-->
    <el-form ref="queryForm" size="small" :inline="true"  label-width="70px">

      <el-date-picker
        v-model="seldate"
        type="month"
        placeholder="选择月">
      </el-date-picker>

      <el-button plain size="mini" style="margin-left: 20px" @click="addpaiban" icon="el-icon-plus" type="primary">新增</el-button>
      <el-button plain size="mini" style="margin-left: 20px" @click="delbyDay" icon="el-icon-delete" type="danger">删除所选</el-button>
      <el-button plain size="mini" style="margin-left: 20px" @click="clickGeneratePicture" icon="el-icon-download" type="warning">保存图片</el-button>
    </el-form>

    <!--  <el-calendar></el-calendar>-->
    <div class="calendar"  style=""  ref="imageDom">
      <el-calendar id="" v-model="seldate" v-loading="loading">
        <template slot="dateCell" slot-scope="{date, data}">
          <!--自定义内容-->
          <div class="calendar-day" style="text-align: center" @click="ShowSelectedDay(data.day)">
            <div class="daytop">
                  <span :class="(date.getDay()==6 || date.getDay()==0)? 'weeked' :'notweeked'">
                   {{ data.day.split('-').slice(1).join('-') }}
                  </span>
              <!--              <i class="el-icon-edit"></i>-->
            </div>
            <div v-for="(item,index) in list" :key="index" >
              <!-- 加数据 -->
              <div v-if="data.day == item.date" style="width:fit-content;float: left;height: 20px;margin-left: 10px;">
                <p @click="" style="text-align: left;line-height: 5px">
                  <span  :style="(item.qiandaoStatus==1)?'color:green':'color:red'">{{item.yuangongName}}</span>
                </p>
              </div>
            </div>
          </div>
        </template>
      </el-calendar>
    </div>

    <!-- 添加或修改排班表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px"  append-to-body>
      <el-form ref="form" :model="addform" :rules="rules" label-width="100px" >
        <el-form-item label="日期" prop="date">
          <el-date-picker clearable
                          type="date"
                          v-model="addform.date"
                          :disabled="isdetailday"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="姓名"  prop="yuangongName" style="width: 90%">
          <el-input v-model="addform.yuangongName"  placeholder="请输入" />
        </el-form-item>

        <el-form-item label="备注"  prop="beizhu" style="width: 90%">
          <el-input v-model="addform.beizhu" placeholder="请输入" />
        </el-form-item>

      </el-form>

      <el-button plain size="mini" icon="el-icon-plus" type="primary" @click="addpaibansubmit" style="float: right;margin-bottom: 10px;margin-left: 10px" v-if="isdetailday">添加</el-button>
      <el-button plain size="mini" icon="el-icon-delete" type="danger" @click="delbyDay" style="float: right;margin-bottom: 10px" v-if="isdetailday">全部删除</el-button>


      <el-table :data="paibandetailList" v-if="isdetailday">
        <el-table-column label="姓名" align="center" prop="yuangongName" />
        <el-table-column label="排班日期" align="center" prop="date" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="beizhu" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParamsbyday.pageNum"
        :limit.sync="queryParamsbyday.pageSize"
        @pagination="selectlistbyday"
      />

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addpaibansubmit" v-if="!isdetailday">确 定</el-button>
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>

  </div>

</template>

<script>
import html2canvas from "html2canvas";
import { addPaibanbiao, delPaibanbiao, delPaibanbiaobyDay, listPaibanbiao, listPaibanbiaobyday } from '@/api/yggl/pbgl'
import { parseTime } from '@/utils/ruoyi'

export default {
  name:"Paiban",
  data() {
    return {
      loading: true,
      total: 0,
      paibandetailList:[],
      isdetailday:false,
      open:false,
      title: "",
      seldate: new Date(),
      editform: {
        paibanname: "null"
      },
      rules: {
        yuangongName: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },
      list:[],
      queryParams: {
        date: null,
      },
      queryParamsbyday: {
        date: null,
        pageNum:1,
        pageSize:10
      },
      delform:{
        date:null
      },
      addform:{
        date:null,
        yuangongName:null,
        beizhu:null
      }

    }
  },
  created() {
  },
  watch: {
    seldate: {
      handler(newdate, olddate) {
        if(parseTime(olddate, '{y}-{m}')==parseTime(newdate, '{y}-{m}')){
          return;
        }
        this.queryParams.date = parseTime(newdate, '{y}-{m}-{d}');
        this.selectlist();
      },
      immediate: true
    }
  },
  methods: {
    ShowSelectedDay(date){
      this.addform.date = date;
      this.open = true;
      this.isdetailday = true;
      this.title=date+"排班人员";
      this.queryParamsbyday.date = date;
      this.selectlistbyday();
    },
    submitForm(){},
    // 取消按钮
    cancel() {
      this.open = false;
    },
    clickGeneratePicture() { //生成图片
      html2canvas(this.$refs.imageDom).then(canvas => {
        // 转成图片，生成图片地址
        this.imgUrl = canvas.toDataURL("image/png"); //可将 canvas 转为 base64 格式
        var eleLink = document.createElement("a");
        eleLink.href = this.imgUrl; // 转换后的图片地址
        eleLink.download = "员工排班表";
        document.body.appendChild(eleLink);
        // 触发点击
        eleLink.click();
        // 然后移除
        document.body.removeChild(eleLink);
      });
    },
    addpaiban(){
      this.resetaddform();
      this.addform.date = new Date();
      this.title = "新增排班人员";
      this.isdetailday = false;
      this.open = true;
    },
    addpaibansubmit(){
      this.$refs["form"].validate(valid => {
        if (valid) {
          addPaibanbiao(this.addform).then(response => {
            this.$modal.msgSuccess("新增成功");
            if (this.isdetailday) {
              this.queryParamsbyday.date = this.addform.date;
              this.selectlistbyday();
            } else {
              this.open = false;
            }
            this.queryParams.date = parseTime(this.seldate, '{y}-{m}-{d}');
            this.selectlist();
            this.resetaddform();
          });
        }
      })
    },
    delbyDay(){
      this.delform.date = this.seldate;
      this.$confirm('确认删除'+parseTime(this.delform.date, '{y}-{m}-{d}')+'的排班数据？').then(
        ()=>{
          this.delform.date = this.seldate;
          delPaibanbiaobyDay(this.delform).then(response=>{
            this.$modal.msgSuccess("删除成功");
            this.selectlist();
            if(this.isdetailday){
              this.queryParamsbyday.date = this.addform.date;
              this.selectlistbyday();
            }
          });
        }
      );
    },
    selectlist(){
      this.loading = true;
      listPaibanbiao(this.queryParams).then(response => {
        this.list =response.rows;
        this.loading = false;
      });
    },
    selectlistbyday(){
      listPaibanbiaobyday(this.queryParamsbyday).then(response => {
        this.paibandetailList =response.rows;
        this.total = response.total;
      });
    },
    resetaddform(){
        // this.addform.date=null;
        this.addform.yuangongName=null;
        this.addform.beizhu=null;
    },
    handleDelete(row){
      const id = row.id;
      delPaibanbiao(id).then(re=>{
        this.$modal.msgSuccess("删除成功");
        this.queryParamsbyday.date = this.addform.date;
        this.selectlistbyday();
        this.selectlist();
      });
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card{
  padding:0 20px;
}
.calendar{
  margin: auto;
  display: flex;
  .left{
    display: flex;
    flex-direction: column;
    div{
      width: 100px;
      //border: 1px solid #dbdbdb;
      text-align: center;
      line-height: 40px;
      cursor: pointer;
    }
    .active{
      color: #2c6ed0;
      font-weight: bold;
    }
  }
}
</style>
<style lang="scss">
.el-carousel__indicators--horizontal {
  bottom: 0;
  // left: 100px;
  transform: translateX(-50%);
  width: 100%;
  display: flex;
  justify-content: center;
}
.calendar{

  p{
    font-size: 15px;
    //  line-height: 20px;
  }
  .el-calendar__header{
    padding: 6px 10px;
  }
  .el-calendar-table{
    border-collapse:collapse !important;
  }
  .el-calendar-table .el-calendar-day{
    padding:5px 3px !important;
    height: 110px !important;
    overflow: hidden;
  }
  .el-calendar-table thead th{
    font-weight: bold;
    font-size: 16px !important;
    color: #1e1e1e;
  }
  .el-calendar__body{
    padding: 5px !important;
  }
  .el-calendar-table thead th{
    padding:5px !important;
    font-size: 12px;
  }
  #calendar{

    .el-button-group>.el-button:not(:first-child) {
      margin-right: -1px;
      display: none;
    }

  }
  .daytop{
    display: flex;
    width: 100%;
    padding: 5px 10px;
    justify-content: space-between;
    span{
      font-size: 14px;
      font-weight: bold;
    }
    i{
      font-size: 14px;
    }
  }

  div.el-calendar {

    .el-calendar-table:not(.is-range) td{
      border: 1px solid #dbdbdb;
    }
    .el-calendar-table:not(.is-range) td.next {
      /*隐藏下个月的日期*/
      display: none;
    }
    .el-calendar-table:not(.is-range) td.prev {
      /*隐藏上个月的日期*/
      visibility: hidden;
      border-left: none;
      border-right: none;
    }
  }

}
</style>
